
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Tathata.Common.BLL.Entities;
using Tathata.Common.BLL.Managers;
using Tathata.Common.BLL.EntityCollections;
using System.Collections.Generic;

namespace Tathata.Web.Administration
{
    public partial class Administration_CommonSettings : BaseNopAdministrationPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                FillDropDowns();
                BindData();
            }
        }

        private void BindData()
        {
            txtStoreName.Text = SettingManager.GetSettingValue("Common.StoreName");
            txtStoreURL.Text = SettingManager.GetSettingValue("Common.StoreURL");
            cbSSLIsInstalled.Checked = SettingManager.GetSettingValueBoolean("Common.StoreHasSSLInstalled");
            
            txtDefaulSEOTitle.Text = SettingManager.GetSettingValue("SEO.DefaultTitle");
            txtDefaulSEODescription.Text = SettingManager.GetSettingValue("SEO.DefaultMetaDescription");
            txtDefaulSEOKeywords.Text = SettingManager.GetSettingValue("SEO.DefaultMetaKeywords");
            
            txtStoreAdminEmail.Text = MessageManager.AdminEmailAddress;
            txtStoreAdminHost.Text = MessageManager.AdminEmailHost;
            txtStoreAdminPort.Text = MessageManager.AdminEmailPort.ToString();
            txtStoreAdminUser.Text = MessageManager.AdminEmailUser;
            txtStoreAdminPassword.Text = MessageManager.AdminEmailPassword;
            cbEnableSsl.Checked = MessageManager.AdminEmailEnableSsl;
            cbUseDefaultCredentials.Checked = MessageManager.AdminEmailUseDefaultCredentials;

            cbEnableCompareProducts.Checked = SettingManager.GetSettingValueBoolean("Common.EnableCompareProducts");
            cbEnableEmailAFriend.Checked = SettingManager.GetSettingValueBoolean("Common.EnableEmailAFirend");
            cbStoreClosed.Checked = SettingManager.GetSettingValueBoolean("Common.StoreClosed");
        }

        private void FillDropDowns()
        {
            MeasureWeight baseWeight = MeasureWeightManager.BaseWeightIn;
            SettingManager.GetSettingValueInteger("Common.BaseWeightIn");
            this.ddlBaseWeight.Items.Clear();
            MeasureWeightCollection measureWeightCollection = MeasureWeightManager.GetAll();
            foreach (MeasureWeight measureWeight in measureWeightCollection)
            {
                ListItem ddlMeasureWeightItem2 = new ListItem(measureWeight.Name, measureWeight.MeasureWeightID.ToString());
                this.ddlBaseWeight.Items.Add(ddlMeasureWeightItem2);
                if (baseWeight != null && measureWeight.MeasureWeightID == baseWeight.MeasureWeightID)
                    ddlMeasureWeightItem2.Selected = true;
            }

            MeasureDimension baseDimension = MeasureDimensionManager.BaseDimensionIn;
            this.ddlBaseDimension.Items.Clear();
            MeasureDimensionCollection measureDimensionCollection = MeasureDimensionManager.GetAll();
            foreach (MeasureDimension measureDimension in measureDimensionCollection)
            {
                ListItem ddlMeasureDimensionItem2 = new ListItem(measureDimension.Name, measureDimension.MeasureDimensionID.ToString());
                this.ddlBaseDimension.Items.Add(ddlMeasureDimensionItem2);
                if (baseDimension != null && measureDimension.MeasureDimensionID == baseDimension.MeasureDimensionID)
                    ddlMeasureDimensionItem2.Selected = true;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    SettingManager.SetParam("Common.StoreName", txtStoreName.Text, string.Empty);
                    SettingManager.SetParam("Common.StoreURL", txtStoreURL.Text, string.Empty);
                    SettingManager.SetParam("Common.StoreHasSSLInstalled", cbSSLIsInstalled.Checked.ToString(), string.Empty);

                    SettingManager.SetParam("SEO.DefaultTitle", txtDefaulSEOTitle.Text, string.Empty);
                    SettingManager.SetParam("SEO.DefaultMetaDescription", txtDefaulSEODescription.Text, string.Empty);
                    SettingManager.SetParam("SEO.DefaultMetaKeywords", txtDefaulSEOKeywords.Text, string.Empty);

                    MessageManager.AdminEmailAddress = txtStoreAdminEmail.Text;
                    MessageManager.AdminEmailHost = txtStoreAdminHost.Text;
                    if (!String.IsNullOrEmpty(txtStoreAdminPort.Text))
                        MessageManager.AdminEmailPort = int.Parse(txtStoreAdminPort.Text);
                    else
                        MessageManager.AdminEmailPort = 0;
                    MessageManager.AdminEmailUser = txtStoreAdminUser.Text;
                    MessageManager.AdminEmailPassword = txtStoreAdminPassword.Text;
                    MessageManager.AdminEmailEnableSsl = cbEnableSsl.Checked;
                    MessageManager.AdminEmailUseDefaultCredentials = cbUseDefaultCredentials.Checked;

                    SettingManager.SetParam("Common.StoreClosed", cbStoreClosed.Checked.ToString(), string.Empty);
                    SettingManager.SetParam("Common.EnableCompareProducts", cbEnableCompareProducts.Checked.ToString(), string.Empty);
                    SettingManager.SetParam("Common.EnableEmailAFirend", cbEnableEmailAFriend.Checked.ToString(), string.Empty);

                    int baseWeightID =int.Parse(ddlBaseWeight.SelectedItem.Value);
                    MeasureWeightManager.BaseWeightIn = MeasureWeightManager.GetByMeasureWeightID(baseWeightID);
                    int baseDimensionID = int.Parse(ddlBaseDimension.SelectedItem.Value);
                    MeasureDimensionManager.BaseDimensionIn = MeasureDimensionManager.GetByMeasureDimensionID(baseDimensionID);
              
                    Response.Redirect("CommonSettings.aspx");
                }
                catch (Exception exc)
                {
                    ProcessException(exc);
                }
            }
        }
       
        protected void btnSendTestEmail_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    List<string> to = new List<string>();
                    to.Add(txtSendEmailTo.Text);
                    MessageManager.SendEmail("Test message", "Test message", MessageManager.AdminEmailAddress, to);
                    lblSendTestEmailResult.Text = "Email has been successfully sent.";
                }
                catch (Exception exc)
                {
                    lblSendTestEmailResult.Text = exc.Message;
                }
            }
        }
    }
}
